/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package handlers;

import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;

/**
 * 
 * This class is specific to the software check. It will hash all the values 
 * and provide a means to check to see if the test fails or not.
 * 
 * @author Royce Feng
 */

public class JKSoftwareTestHandler {

    private HashMap<String,Boolean> softwareHash = new HashMap<String,Boolean>();

    /**
     *
     * Main constructor for the class. Sets the default hashed values to false.
     *
     * @param keys The hash keys
     */
    public JKSoftwareTestHandler( ArrayList<String[]> keys ) {
        for ( int i = 0; i < keys.size(); i++ ) {
            softwareHash.put(keys.get(i)[0], false);
        }
    }
    
    /**
     * 
     * Hashes a value
     * 
     * @param key The hash key
     * @param val The value to be hashed
     */
    public void setHash ( String key, boolean val ) {
        softwareHash.put(key, val);
    }
    
    /**
     * 
     * Shows the hashed value for a certain key.
     * 
     * @param key The hash key
     * @return The hashed value if the key exists. False if it does not.
     */
    public boolean viewHash( String key ) {
        boolean returnval = softwareHash.get(key);
        if ( softwareHash.get(key) == null ) {
            return false;
        } else {
            return returnval;
        }
    }
    
    /**
     * 
     * Returns the result of the software check.
     * 
     * @return True if all tests passed. False if at least one fails.
     */
    public boolean getResult() {
        Iterator<String> iter = softwareHash.keySet().iterator();
        boolean returnval = true;
        while ( iter.hasNext() ) {
            String key = iter.next();
            returnval = returnval && softwareHash.get(key);
        }
        return returnval;
    }
    
    /**
     * 
     * Provides a means to see the hashmap.
     * 
     * @return The lexicographic interprettation of the HashMap
     */
    @Override
    public String toString() {
        String base = "";
        Iterator<String> iter = softwareHash.keySet().iterator();
        while ( iter.hasNext() ) {
            String key = iter.next();
            boolean value = softwareHash.get(key);
            base += "[" + key + "," + value + "]";
        }        
        return base;
    }
    
}
